package com.qupai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qupai.constant.StatusConstant;
import com.qupai.domain.dto.AuctionDTO;
import com.qupai.domain.entity.Auction;
import com.qupai.domain.entity.AuctionItem;
import com.qupai.domain.entity.Auctioneer;
import com.qupai.domain.entity.Orders;
import com.qupai.domain.vo.AuctionDetailVO;
import com.qupai.domain.vo.AuctionItemVO;
import com.qupai.domain.vo.AuctionVO;
import com.qupai.exception.AuctioneerException;
import com.qupai.mapper.AuctionItemMapper;
import com.qupai.mapper.AuctionMapper;
import com.qupai.mapper.AuctioneerMapper;
import com.qupai.mapper.OrdersMapper;
import com.qupai.service.IAuctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.utils.BaseContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 拍卖表，存储拍卖活动信息 服务实现类
 * </p>
 *
 * @author bluefoxyu
 * @since 2024-07-30
 */
@Service
@RequiredArgsConstructor
public class AuctionServiceImpl extends ServiceImpl<AuctionMapper, Auction> implements IAuctionService {

    private final AuctionMapper auctionMapper;
    private final AuctioneerMapper auctioneerMapper;
    private final AuctionItemMapper auctionItemMapper;
    private final OrdersMapper ordersMapper;

    @Transactional
    @Override
    public Auction hold(AuctionDTO auctionDTO) {

        //检查该拍卖师是否还有举办拍卖会的次数
        QueryWrapper<Auctioneer>auctioneerQueryWrapper=new QueryWrapper<>();
        auctioneerQueryWrapper.lambda().eq(Auctioneer::getId, BaseContext.getCurrentId());
        Auctioneer auctioneer = auctioneerMapper.selectOne(auctioneerQueryWrapper);
        if (auctioneer==null||auctioneer.getChance()==0){
            throw new AuctioneerException("该拍卖师以无举办拍卖会次数");
        }

        Auction auction = Auction.builder()
                .name(auctionDTO.getName())
                .startTime(auctionDTO.getStartTime())
                .endTime(auctionDTO.getEndTime())
                .auctioneerId(BaseContext.getCurrentId())
                .status(0)//默认待开始
                .build();
        auctionMapper.insert(auction);

        //更新次数
        Integer chance = auctioneer.getChance();
        chance=chance-1;
        auctioneer.setChance(chance);
        auctioneerMapper.updateById(auctioneer);

        return auction;
    }

    @Override
    public Auction updateAuction(AuctionDTO auctionDTO) {
        Auction auction = Auction.builder()
                .id(auctionDTO.getId())
                .name(auctionDTO.getName())
                .startTime(auctionDTO.getStartTime())
                .endTime(auctionDTO.getEndTime())
                .build();
        auctionMapper.updateById(auction);
        return auction;
    }

    @Transactional
    public List<AuctionVO> find(AuctionDTO auctionDTO) {

        QueryWrapper<Auction> auctionQueryWrapper = new QueryWrapper<>();
        auctionQueryWrapper.lambda()
                .like(auctionDTO.getName() != null, Auction::getName,auctionDTO.getName())
                .ge(auctionDTO.getStartTime() != null, Auction::getStartTime,auctionDTO.getStartTime())
                .le(auctionDTO.getEndTime() != null, Auction::getEndTime,auctionDTO.getEndTime())
                .eq(Auction::getAuctioneerId, BaseContext.getCurrentId())
                .orderByAsc(Auction::getStartTime);

        List<Auction> auctions = auctionMapper.selectList(auctionQueryWrapper);
        List<AuctionVO> auctionVOList = new ArrayList<>();

        if(auctions != null) {
            for(Auction auction:auctions){
                AuctionVO auctionVO = new AuctionVO();
                BeanUtils.copyProperties(auction, auctionVO);
                auctionVOList.add(auctionVO);
            };

            for(AuctionVO auctionVO : auctionVOList) {

                QueryWrapper<AuctionItem>itemQueryWrapper=new QueryWrapper<>();
                itemQueryWrapper.lambda()
                        .eq(AuctionItem::getAuctionId, auctionVO.getId())
                        .eq(AuctionItem::getAuctionStatus, StatusConstant.ENABLE);

                List<AuctionItem> auctionItems = auctionItemMapper.selectList(itemQueryWrapper);
                List<String> images = new ArrayList<>();

                for(AuctionItem auctionItem :auctionItems) {
                    images.add(auctionItem.getImage());
                }

                auctionVO.setImage(images);
            };

        }


        return auctionVOList;
    }


    //TODO 屎山 心情好了改
    @Override
    public AuctionDetailVO detail(Long id) {
        Auction auction = auctionMapper.selectById(id);

        AuctionDetailVO auctionDetailVO = new AuctionDetailVO();
        BeanUtils.copyProperties(auction,auctionDetailVO);

        QueryWrapper<AuctionItem> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AuctionItem::getAuctionId,auction.getId());
        List<AuctionItem> auctionItems = auctionItemMapper.selectList(wrapper);

        List<AuctionItemVO>auctionItemVOList = new ArrayList<>();

        //查询商品
        for(AuctionItem auctionItem : auctionItems)
        {
            AuctionItemVO auctionItemVO = new AuctionItemVO();
            BeanUtils.copyProperties(auctionItem,auctionItemVO);

            //拍卖状态(1、待拍卖, 2、拍卖中, 已拍卖(3、待发货 4、已发货 5、完成), 6、逾期未拍, 7、未上架)
            if (auctionDetailVO.getStatus() == 3){
                QueryWrapper<Orders> orderQueryWrapper = new QueryWrapper<>();
                orderQueryWrapper.lambda().eq(Orders::getId,auctionItem.getId());

                Orders orders = ordersMapper.selectOne(orderQueryWrapper);

                auctionItemVO.setStatus(orders.getStatus());
            }

            auctionItemVOList.add(auctionItemVO);
        }

        auctionDetailVO.setAuctionItemList(auctionItemVOList);

        return auctionDetailVO;
    }


}
